package com.ruoyi.hospital.controller.prescriptionInfo;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hospital.domain.prescriptionInfo.PrescriptionDrug;
import com.ruoyi.hospital.service.prescriptionInfo.IPrescriptionDrugService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.hospital.domain.prescriptionInfo.PrescriptionInfo;
import com.ruoyi.hospital.service.prescriptionInfo.IPrescriptionInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 处方信息Controller
 * 
 * @author wendao
 * @date 2024-08-20
 */
@RestController
@RequestMapping("/hospital/prescriptionInfo")
public class PrescriptionInfoController extends BaseController
{
    @Autowired
    private IPrescriptionInfoService prescriptionInfoService;
    @Autowired
    private IPrescriptionDrugService iPrescriptionDrugService;
    /**
     * 查询处方信息列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:prescriptionInfo:list')")
    @GetMapping("/list")
    public TableDataInfo list(PrescriptionInfo prescriptionInfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            prescriptionInfo.setStoreId(getStoreId());
        }
        startPage();
        List<PrescriptionInfo> list = prescriptionInfoService.selectPrescriptionInfoList(prescriptionInfo);

        return getDataTable(list);
    }

    /**
     * 导出处方信息列表
     */
    @PreAuthorize("@ss.hasPermi('hospital:prescriptionInfo:export')")
    @Log(title = "处方信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PrescriptionInfo prescriptionInfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            prescriptionInfo.setStoreId(getStoreId());
        }
        List<PrescriptionInfo> list = prescriptionInfoService.selectPrescriptionInfoList(prescriptionInfo);
        ExcelUtil<PrescriptionInfo> util = new ExcelUtil<PrescriptionInfo>(PrescriptionInfo.class);
        util.exportExcel(response, list, "处方信息数据");
    }

    /**
     * 获取处方信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:prescriptionInfo:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        //return success(prescriptionInfoService.selectPrescriptionInfoById(id));
        PrescriptionInfo prescriptionInfo = prescriptionInfoService.selectPrescriptionInfoById(id);
        //根据id查询药品信息
        PrescriptionDrug prescriptionDrug = new PrescriptionDrug();
        prescriptionDrug.setPid(prescriptionInfo.getId());
        prescriptionInfo.setPrescriptionDrugs(iPrescriptionDrugService.selectPrescriptionDrugList(prescriptionDrug));
        return success(prescriptionInfo);
    }

    /**
     * 新增处方信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:prescriptionInfo:add')")
    @Log(title = "处方信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PrescriptionInfo prescriptionInfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            prescriptionInfo.setStoreId(getStoreId());
        }
        return toAjax(prescriptionInfoService.insertPrescriptionInfo(prescriptionInfo));
    }

    /**
     * 修改处方信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:prescriptionInfo:edit')")
    @Log(title = "处方信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PrescriptionInfo prescriptionInfo)
    {
        /*用于药店端的查询*/
        if(!StringUtils.isNullOrZero(getStoreId())){
            prescriptionInfo.setStoreId(getStoreId());
        }
        return toAjax(prescriptionInfoService.updatePrescriptionInfo(prescriptionInfo));
    }

    /**
     * 删除处方信息
     */
    @PreAuthorize("@ss.hasPermi('hospital:prescriptionInfo:remove')")
    @Log(title = "处方信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(prescriptionInfoService.deletePrescriptionInfoByIds(ids));
    }
}
